<?php
/**
 * Created by PhpStorm.
 * User: sen
 * Date: 16/7/11
 * Time: 上午11:46
 */
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\User;
use app\models\LoginForm;


class LoginController extends Controller{

    //登录界面
    public function actionIndex(){

        $request = \Yii::$app->request;
        $url = $request->get('url');

        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['index.php/publish/index']);
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $userId = \Yii::$app->user->id;
            $user = new User();
            $admin = $user->findIdentity($userId);
            if($admin['type']==2){
                if($url){
                    $backUrl= '../'.$url;
                }else{
                    $backUrl= '../publish/index';
                }
                return $this->redirect($backUrl);//跳转到商家界面
            }elseif($admin['type']==1){
                if($url){

                    $backUrl= '../'.$url;
                }else{
                    $backUrl= '../lowgrade/mybroke';
                }
                return $this->redirect($backUrl);//跳转到代理商界面

            }else{
                return $this->renderPartial('login', [
                    'model' => $model,
                ]);
            }
        }
        return $this->renderPartial('login', [
            'model' => $model,
        ]);

    }

    //退出登录
    public function actionLogout(){
        \Yii::$app->user->logout();
        return $this->redirect(['index.php/login/index']);
    }


}